

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.mobilerobots.Aria.Aria;



public class InterfazProyecto extends JFrame implements ActionListener,Observer{
	
	private ThreadRobot threadRobotA;
	
	private ThreadRobot threadRobotB;
	
	private JMenuBar menu;
	
	private JMenu menuMundos;
	
	//private JMenu menuPlanificacion;
	
	private JMenu menuSimulacion;
	
	//private JMenuItem itemPlanificacion;
	
	private JMenuItem itemSimulacion;
	
	private JEditorPane worldDescription;
	
	private JEditorPane worldSolutionA;
	
	private JEditorPane worldSolutionB;
	
	private JEditorPane console;
	
	private String selectedWorld;
	
	private Planificacion planificacion;
	
	private String[] args;
	
	private JComboBox comboRobot;
	
	public InterfazProyecto(String[] args){
		Proyecto1.getInstance().addObserver(this);
		this.args=args;
		planificacion = new Planificacion();
		setTitle("Robot Planner");
		setSize(800,800);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		menu = new JMenuBar();
		menuMundos = new JMenu("Mundos");
		/*menuPlanificacion = new JMenu("Planificacion");
		itemPlanificacion = new JMenuItem("Planificar");
		itemPlanificacion.setActionCommand("Planificar");
		itemPlanificacion.addActionListener(this);
		menuPlanificacion.add(itemPlanificacion);*/
		menuSimulacion = new JMenu("Simulacion");
		itemSimulacion = new JMenuItem("Simulacion");
		itemSimulacion.setActionCommand("Simulacion");
		itemSimulacion.addActionListener(this);
		menuSimulacion.add(itemSimulacion);
		fillMenu();
		menu.add(menuMundos);
		//menu.add(menuPlanificacion);
		menu.add(menuSimulacion);
		comboRobot = new JComboBox();
		comboRobot.addItem("P3DX");
		comboRobot.addItem("Amigo");
		setJMenuBar(menu);
		JPanel panelTmp = new JPanel();
		JPanel panelTmp2 = new JPanel();
		JPanel panelLeft = new JPanel();
		JPanel panelRight = new JPanel();
		JPanel panelSouth = new JPanel();
		worldDescription = new JEditorPane();
		worldDescription.setPreferredSize(new Dimension(760,200));
		worldDescription.setFont(new Font("Monospaced",Font.PLAIN,13));
		worldSolutionA = new JEditorPane();
		worldSolutionA.setPreferredSize(new Dimension(760,200));
		worldSolutionA.setFont(new Font("Monospaced",Font.PLAIN,11));
		/*worldSolutionB = new JEditorPane();
		worldSolutionB.setPreferredSize(new Dimension(560,200));
		worldSolutionB.setFont(new Font("Monospaced",Font.PLAIN,11));*/
		console = new JEditorPane();
		console.setPreferredSize(new Dimension(760,160));
		panelLeft.add(new JScrollPane(worldDescription));
		panelLeft.setPreferredSize(new Dimension(800,250));
		panelLeft.setBorder(BorderFactory.createTitledBorder("World Description"));
		panelRight.add(new JScrollPane(worldSolutionA),BorderLayout.EAST);
		//panelRight.add(new JScrollPane(worldSolutionB),BorderLayout.WEST);
		panelRight.setPreferredSize(new Dimension(800,250));
		panelRight.setBorder(BorderFactory.createTitledBorder("World Solution"));
		panelSouth.add(new JScrollPane(console),BorderLayout.SOUTH);
		panelSouth.setPreferredSize(new Dimension(800,200));
		panelSouth.setBorder(BorderFactory.createTitledBorder("Console"));
		panelTmp.setLayout(new BorderLayout());
		panelTmp2.setLayout(new BorderLayout());
		panelTmp2.add(new JLabel("Selected Robot"),BorderLayout.WEST);
		panelTmp2.add(comboRobot,BorderLayout.EAST);
		panelTmp.add(panelTmp2,BorderLayout.NORTH);
		panelTmp.add(panelLeft,BorderLayout.CENTER);
		add(panelTmp,BorderLayout.NORTH);
		add(panelRight,BorderLayout.CENTER);
		add(panelSouth,BorderLayout.SOUTH);
		
	}
	
	private void fillMenu(){
		File f = new File("./");
		MyFileFilter filter = new MyFileFilter(3);
		File[] files = f.listFiles(filter);
		for (int i = 0; i < files.length; i++) {
			JMenuItem item = new JMenuItem(files[i].getName().substring(10));
			item.setActionCommand("world:"+files[i].getName());
			item.addActionListener(this);
			menuMundos.add(item);
		}
	}

	
	public static void main(String[] args) {
		new InterfazProyecto(args).setVisible(true);
	}
	
	class MyFileFilter implements FileFilter{

		private int problema;
		
		
		
		public MyFileFilter(int problema) {
			super();
			this.problema = problema;
		}

		@Override
		public boolean accept(File arg0) {
			return arg0.getName().startsWith("problema"+problema+"-mundo")&& arg0.isDirectory();
		}


		
	}

	private void loadFile(JEditorPane editor, File f){
		try{
			String contents = "";
			BufferedReader bf = new BufferedReader(new FileReader(f));
			String linea = bf.readLine();
			while(linea!=null){
				contents+= linea+"\n";
				linea = bf.readLine();
			}
			editor.setText(contents);
			editor.setEditable(false);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String cmd = arg0.getActionCommand();
		if(cmd.startsWith("world:")){
			cmd = cmd.split(":")[1];
			File f = new File("./"+cmd+"/"+cmd+"-7x17.txt");
			selectedWorld = cmd;
			loadFile(worldDescription, f);
			try{
				File f1 = new File("./"+selectedWorld+"/"+selectedWorld+"-7x17.txt");
				planificacion.loadMap(f1);
				
				File f2;
				if(comboRobot.getSelectedIndex()==0){
					f2 = new File("./"+selectedWorld+"/"+selectedWorld+"-7x17-camino-A.txt");
					planificacion.findPoints("./"+selectedWorld+"/"+selectedWorld+"-7x17-puntos-A.txt",0);						
					loadFile(worldSolutionA, f2);					
				}else{
					f2 = new File("./"+selectedWorld+"/"+selectedWorld+"-7x17-camino-B.txt");
					planificacion.findPoints("./"+selectedWorld+"/"+selectedWorld+"-7x17-puntos-B.txt",1);
					loadFile(worldSolutionA, f2);					
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		
		}else if(cmd.equals("Simulacion")){
			String[] myArgs;
			String file;
			if(comboRobot.getSelectedIndex()==0){
				myArgs = new String[]{"-laserType","urg","-rrtp","8101"};
				file = "./"+selectedWorld+"/"+selectedWorld+"-7x17-puntos-A.txt";
				threadRobotA = new ThreadRobot(myArgs,file );
				threadRobotA.start();
			}else{
				myArgs = new String[]{"-rrtp","8102"};
				file = "./"+selectedWorld+"/"+selectedWorld+"-7x17-puntos-B.txt";
				threadRobotB = new ThreadRobot(myArgs,file );
				threadRobotB.start();
			}
			/**/
			
		}
	}

	
	
	@Override
	public void update(Observable arg0, Object arg1) {
		console.setText(console.getText()+"\n"+arg1);
	
		
	}
	
	
	
	public class ThreadRobot extends Thread
	{
		private String[] argv;
		private String mundo;
		
		public ThreadRobot(String[] args, String mun)
		{
			argv = args;
			mundo = mun;
		}
		
		public void run(){
			Proyecto1.run(argv, mundo);
		}
		

	}
}

